package com.example.mytayunapi.Controller;


import com.example.mytayunapi.Entity.adminInfo;
import com.example.mytayunapi.Mapper.adminMapper;
import com.example.mytayunapi.Service.adminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("api")

public class adminController {


    @Autowired
    private adminService adminservice;

    //111
    @PostMapping("/edit")
    public Integer updateadminInfo(@RequestBody adminInfo adminIfo) {
        Integer i = adminservice.updateadminInfo(adminIfo);
        return i;
    }


    @GetMapping("/del/{School_degree}")
    private Integer deladmin(@PathVariable String School_degree) {
        return adminservice.deleteBySchool_degree(School_degree);
    }

    @GetMapping("/query")
    public List<adminInfo> queryadminInfoBySchool_degree(String School_degree) {
        return adminservice.getAlladminInfo(School_degree);
    }

    @PostMapping("/add")
    public Integer addadminInfo(adminInfo adminIfo) {
        Integer i =  adminservice.insertadminInfo(adminIfo);
        return i;
    }
    
    @PostMapping("/login")
    public ResponseEntity<?> login( @RequestParam(value = "School_degree", required = false) String School_degree,
                                    @RequestParam(value = "password", required = false) String password) {
        if (School_degree == null || password == null) {
            return new ResponseEntity<>("未填写登录信息", HttpStatus.BAD_REQUEST);
        }

        // 检查学号和密码是否正确
        adminInfo admin = adminservice.findBySchool_degreeAndPassword(School_degree, password);
        if (admin != null) {
            return new ResponseEntity<>("登录成功", HttpStatus.OK);
        } else {
            // 这里可以根据需要返回更具体的错误信息，例如：
            // return new ResponseEntity<>("学号或密码错误", HttpStatus.UNAUTHORIZED);
            return new ResponseEntity<>("登录失败", HttpStatus.UNAUTHORIZED);
        }

    }
}
